package edu.wayne.cs.csc4110.hw6.framework.plugin;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;


public class PluginLoader {

	private InputStream in;
	private Reader fileReader;
	private BufferedReader bufReader;
	private List<String> pluginStrings = new ArrayList<String>();
	public PluginLoader() {	
		try {
		
		in = getClass().getResourceAsStream("/edu/wayne/cs/csc4110/hw6/plugins/"+"plugins.config");
	
			fileReader = new InputStreamReader(in, "utf-8");
	
		bufReader = new BufferedReader(fileReader);
		String strLine;
		while((strLine = bufReader.readLine())!=null){
			pluginStrings.add(strLine);
		}
		in.close();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public Vector<ActivePlugin> getPlugins(){
		Vector<ActivePlugin> pluginVector= new Vector<ActivePlugin>();
		for(String pluginPackage: pluginStrings){
			ActivePlugin p = null;
			Class<ActivePlugin> c;
			try {
				c = (Class<ActivePlugin>) ClassLoader.getSystemClassLoader().loadClass(pluginPackage);
				p= c.newInstance();
			} catch (ClassNotFoundException e1) {
				System.err.println("Unable to load Class");
				e1.printStackTrace();
			} catch (InstantiationException | IllegalAccessException e) {
				e.printStackTrace();
			}
			 if(p!=null){
				 pluginVector.add(p);
			 }
		}
		return pluginVector;

	}

	
}
